﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FifaChamps.API.Data;

namespace FifaChamps.API.Logic
{
    public class Stage
    {
        public int ID { get; set; }
        public int TournamentID { get; set; }
        public string Name { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
        public StageType StageType { get; private set; }        

        protected Stage()
        {
            ID = default(int);
            TournamentID = default(int);
            Name = default(string);
            StartDate = default(DateTime?);
            EndDate = default(DateTime?);
            StageType = StageType.Unknown;
        }

        protected Stage(int id, int tournamentid, string name, DateTime? startdate, DateTime? enddate, StageType stageType)
            : this()
        {
            ID = id;
            Name = name;
            TournamentID = tournamentid;
            StartDate = startdate;
            EndDate = enddate;
            StageType = stageType;
        }

        public static Stage Factory(int tournamentid, string name, DateTime? startdate, DateTime? enddate, StageType stageType)
        {
            return new Stage(0, tournamentid, name, startdate, enddate, stageType);
        }

        public int Add()
        {
            if (this.ID == default(int))
            {
                this.ID = Add_Stage(this);
                return this.ID;
            }
            return this.ID; 
        }

        public void Save()
        {
            if (this.ID > 0)
            {
                Save_Stage(this);
            }
        }

        public void Add_StagePlayer(int playerID)
        {
            Add_StagePlayer(this.ID, playerID);
        }

        public int Add_Phase(params PhaseParam[] parameters)
        {
            if (this.ID != default(int))
                return Phase.Add_Phase(this.ID, this.StageType, parameters);

            return 0;
        }

        public List<Player> Get_StagePlayers()
        {
            return Get_StagePlayers(this.ID);
        }

        #region Methods [Static]

        public static Dictionary<int, Stage> Get_Stages(int tournamentID)
        {
            var result = Data.DataManager.Get_Stages(tournamentID);
            var stages = new Dictionary<int, Stage>();

            foreach (var item in result)
            {
                var stagetype = Get_StageTypeForStage(item.StageID);
                stages.Add(item.StageID, new Stage(item.StageID, item.TournamentID, item.Name, item.StartDate, item.EndDate, stagetype));
            }

            return stages;
        }

        public static List<Player> Get_StagePlayers(int stageID)
        {
            List<Player> players = new List<Player>();
            var list = DataManager.Get_StagePlayers(stageID);
            foreach (var item in list)
            {
                players.Add(Player.Get_Player(item));
            }
            return players;
        }
        
        private static int Add_Stage(Stage stage)
        {
            return Add_Stage(stage.TournamentID, stage.Name, stage.StartDate, stage.EndDate, (int) stage.StageType);
        }

        private static int Add_Stage(int tournamentid, string stagename, DateTime? startdate, DateTime? enddate, int typeid)
        {
            return DataManager.Add_Stage(tournamentid, stagename, startdate, enddate, typeid);
        }

        private static int Add_StagePlayer(int stageID, int playerID)
        {
            return DataManager.Add_StagePlayer(stageID, playerID);
        }

        private static int Save_Stage(Stage stage)
        {
            return Save_Stage(stage.ID, stage.TournamentID, stage.Name, stage.StartDate, stage.EndDate, (int) stage.StageType);
        }

        private static int Save_Stage(int stageid, int tournamentid, string stagename, DateTime? startdate, DateTime? enddate, int typeID)
        {
            return DataManager.Update_Stage(stageid, tournamentid, stagename, startdate, enddate, typeID);
        }

        private static StageType Get_StageTypeForStage(int stageID)
        {
            if (DataManager.Get_Groups(stageID).Count > 0) return StageType.Group;
            if (DataManager.Get_Seasons(stageID).Count > 0) return StageType.Season;
            if (DataManager.Get_Knockouts(stageID).Count > 0) return StageType.Knockout;
            return StageType.Unknown;
        }

        #endregion

    }
   
    public enum StageType
    {
        Season = 1,
        Group = 2,
        Knockout = 3,
        Unknown = 0
    }
}
